<template>
    <CardBox title="楼盘媒体字典（CPH计算器）" class="bg-color">
        <div class="radio">
            <div class="radio-item" :class="{ 'active': active == 1 }" @click="radioChange(1)">
                电梯媒体
            </div>
            <div class="radio-item" :class="{ 'active': active == 2 }" @click="radioChange(2)">
                门禁
            </div>
        </div>
        <template v-if="active == 1">
            <div class="table">
                <div class="flex title tr">
                    <div v-for="(item) in mediaTypeList" :key="item.name" class="type-item">
                        <p class="name">
                            {{ item.name }}
                        </p>
                    </div>
                </div>
                <div v-for="(item, index) in tableList" :key="item.title" class="flex title tr">
                    <div class="type-item">
                        <p v-if="index !== 0" class="way">
                            {{ item.title }}
                        </p>
                        <p v-if="item.unit" class="unit" :class="index === 0 && 'way'">
                            {{ item.unit }}
                        </p>
                    </div>
                    <template v-for="(td, i) in item" :key="i">
                        <div
                            v-if="i === 'num1' || i === 'num2' || i === 'num3'" class="type-item value"
                            :class="index === 2 && 'red'"
                        >
                            <div>
                                <input
                                    v-if="index === 1" v-model="item[i]" :readonly="!tableList[0][i]" type="number"
                                    @input="onChange(i)"
                                >
                                <span v-else>
                                    {{ item[i] }}
                                </span>
                            </div>
                        </div>
                    </template>
                </div>
            </div>
            <div class="tips">
                <!-- <img src="@/assets/img/cph-tips.png" alt=""> -->
                <div>
                    说明：
                </div>
                <div>
                    <p>1.预估价格可调整，调整后将为你自动计算CPH；</p>
                    <p>2.视频媒体均按15秒300次进行计算；</p>
                </div>
            </div>
        </template>
        <div v-else class="empty">
            <img src="@/assets/img/expect.png" />
        </div>
    </CardBox>
</template>

<script lang="ts"  setup>
import { ref, PropType, watch } from 'vue';
import CardBox from './components/CardBox.vue';
import { mediaTypeList, examplePropertiesInfo } from '@/utils/consts';
import { CphTableItemModel } from '../products/brandBreakout/breakout.module';
import { PremisesInfoModel } from './onePage.module';
import { onMounted } from 'vue';

const props = defineProps({
    info: {
        type: Object as PropType<PremisesInfoModel>,
        require: true,
        default: () => ({}),
    },
});

const active = ref<number>(1);

const radioChange = (key: number) => {
    active.value = key;
};

const tableList = ref<CphTableItemModel[]>(JSON.parse(JSON.stringify(examplePropertiesInfo.table)) as CphTableItemModel[]);

const onChange = (num: string) => {
    if (props.info.coveredHouseholdNum) {
        let price = tableList.value[1][num];
        price = `${price}`.replace(/[^\d.]/g, '');
        if (Number(price) < 0) {
            tableList.value[1][num] = '0';
            return;
        }
        const arr = `${price}`.split('.');
        price = arr.length === 1 ? arr[0].slice(0, 5) : [arr[0].slice(0, 5), arr[1].slice(0, 2)].join('.');
        tableList.value[1][num] = price;

        const estimatedPrice = Number(price);
        tableList.value[2][num] = Math.floor(estimatedPrice * Number(tableList.value[0][num]) / props.info.coveredHouseholdNum * 100) / 100;
    } else {
        tableList.value[2][num] = 0;
    }
};

const initData = () => {
    if (props.info) {
        tableList.value[0].num1 = props.info.smartNum || 0;
        tableList.value[0].num2 = props.info.lcdNum || 0;
        tableList.value[0].num3 = props.info.frameNum || 0;
        onChange('num1');
        onChange('num2');
        onChange('num3');
    }
};

watch(
    () => props.info,
    () => {
        initData();
    }, {
        deep: true
    }
);

onMounted(()=>{
    initData();
});
</script>

<style lang="scss" scoped>
.bg-color {
    background-image: linear-gradient(179deg, #ffeff4 16%, #ffffff 99%);
}

.flex {
    display: flex;
}

.table {
    margin-top: 6px;

    .title {
        justify-content: flex-end;

        .type-item {
            width: 25%;
            text-align: center;
            color: #333333;

            .name {
                height: 20px;
                // font-family: PingFangSC-Medium;
                font-weight: 600;
                font-size: 14px;
            }

            .unit {
                height: 16px;
                // font-family: PingFangSC-Regular;
                font-weight: 400;
                font-size: 12px;
                color: #b5b5b5;
                text-align: center;
                line-height: 16px;
                margin-top: 2px;
            }

            .way {
                height: 17px;
                color: #333333;
                // font-family: PingFangSC-Regular;
                font-weight: 400;
                font-size: 12px;
            }
        }

        .value {
            color: #333333;
            font-size: 14px;
            // font-family: PingFangSC;
            font-weight: 600;

            div {
                margin: 0 10px;
            }
        }

        .red {
            color: #e60044;
        }

        input {
            display: block;
            box-sizing: border-box;
            width: 100%;
            height: 28px;
            min-width: 0;
            margin: 0;
            padding: 0;
            line-height: inherit;
            background: #e6004408;
            border: 0.5px solid #e6004433;
            border-radius: 4px;
            outline: none;
            resize: none;
            user-select: auto;
            caret-color: #e60044;
            font-weight: 600;
            font-size: 14px;
            color: #e60044;
            text-align: center;

            &::placeholder {
                font-weight: 500;
                font-size: 14px;
                color: #f8b9cb;
            }
        }
    }

    .tr {
        padding: 9px 0;
        // border-bottom: 1px solid #f4f4f4;
        align-items: center;
    }
}

.tips {
    margin-top: 12px;
    display: flex;
    font-family: PingFangSC-Regular, sans-serif;
    font-weight: 400;
    font-size: 12px;
    color: #a5adb7;
    align-items: flex-start;
    line-height: 16px;

    img {
        width: 14px;
        height: 14px;
        margin-right: 8px;
        margin-top: 4px;
    }

    p {
        // font-family: PingFangSC-Regular;
        font-weight: 400;
        font-size: 12px;
        line-height: 16px;
    }
}

.radio {
    height: 30px;
    background: #ffffff;
    border: 0.5px solid #e60044;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;

    &-item {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: PingFangSC-Medium, sans-serif;
        font-weight: 500;
        font-size: 13px;
        color: #e60044;

        &.active {
            background: #e60044;
            color: #ffffff;
        }
    }
}

.empty {
    text-align: center;
    margin: 55px auto;

    img {
        width: 189px;
        height: 129px;
    }
}
</style>
